<div class="box altbox">
    <div class="boxin">
        <div class="header">
            <h1>Inbox</h1>
        </div>
        <div class="page">
        	<div id="test">
            <?= $this->partial('partials/message-list.phtml', array('permalink' => 'permalink', 'title' => 'Title')); ?>
            </div>

            <div style="clear: both;"></div>
        </div>
    </div>
</div>

<?php echo $this->dialogContainer(
    'modal',
    '<div id="newTicketContent"></div>',
    array(
        'autoOpen'      => false,
        'draggable'     => true,
        'modal'         => true,
        'resizable'     => false,
        'title'         => 'New Ticket',
        'closeOnEscape' => true,
        'width'			=> '800px',
    	'minHeight'     => 550,
        'position'		=> 'middle',
        'buttons'       => array(
            'Create'	=> new Zend_Json_Expr('function() {
            	document.location=\''.$this->url(array(
                    'module'	 => 'backoffice',
                    'controller' => 'helpdesk',
                    'action'	 => 'index'
                ), "", true) .'\'
            }'),
            'Cancel'    => new Zend_Json_Expr('function() {
                $(this).dialog(\'close\');
            }')
        )
    )
); 
?>

<script>
	var windowHeight = $(window).height();
    var barSubtraction = 240;

    $(document).ready(function() {
        var messageContainerHeight = windowHeight - barSubtraction - 40;
        var ticketTableHeight = windowHeight - barSubtraction;

		$("#test").height(ticketTableHeight);
		$("#messageContainer").height(messageContainerHeight);

		listFolderTickets();
    });

    $("#newTicketButton").click(function() {
        $("#newTicketContent").load('/backoffice/helpdesk/new-ticket/ajax/1');
        $("#modal").dialog('open');
    });

    $("#refreshInbox").click(function() {
		$("div.content table tbody").html('');

		listFolderTickets();
    });

    function listFolderTickets()
    {
		$.getJSON('/backoffice/async/folder-listing', function(json){
		    var row;

			$.each(json, function(i, ticket){
				row += "<tr><td>"+ticket.from+"</td><td>"+ticket.subject+"</td><td>"+ticket.date+"</td><td>&nbsp;</td></tr>";
			});
			
			if($("div.content table tbody tr:first").length)
				$(row).insertBefore("div.content table tbody tr:first");
			else
				$("div.content table tbody").append(row);
		});
    }
</script>



